<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/16
 * Time: 10:30
 */
namespace app\bo\controller;
use think\Controller;
use think\File;
use think\Request;
use think\Db;
use org\Qiniu;
use Qiniu\Auth;// 引入鉴权类
use Qiniu\Storage\UploadManager;// 引入上传类
use Qiniu\Processing\PersistentFop;
require_once('../extend/qiniu/autoload.php');
class Upload extends Base
{
    public function test()
    {
        $task_sheng = -1;
        $task_sheng = $task_sheng==-1 ? 0 : $task_sheng;
        $task_endtime = '2019-04-24 11:57';
        $task_starttime = '2018-12-13 12:12';
        $cb = (strtotime($task_endtime) - strtotime($task_starttime))/3600;
        echo time();
        echo '<br/>';
        echo strtotime($task_endtime);
    }
    /**
     * 上传图片
     */
    public function uploadImgBendi(){
        $file = request()->file('file');
        $dir = ROOT_PATH . 'public' . DS . 'uploads/home/images';
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
        $url =  http_type ();
        $info = $file->move($dir);
        if($info){
            $newName = $info->getSaveName();
            $size = $info->getSize();
            if($size/1024 > 500){
                image_png_size_add($dir . '/'.$newName,$dir . '/'.$newName);
            }
            return json(['code'=>1,'msg'=>$url."/uploads/home/images/{$newName}",'data'=>$dir . '/'.$newName]);
        }else{
            return json(['code'=>100,'msg'=>$file->getError(),'data'=>['src'=> ""]]);
        }
    }
    public function uploadImg(){
        set_time_limit(0);
        $filePath = $_FILES['file']['tmp_name'];
        //取出图片后缀
        $type = explode(".",$_FILES['file']['name']);
        $type = end($type);
        //组装图片名
        $key = md5(time().uuid()).'.'.$type;
        $up = new Qiniu();
        $data = $up->uploadFile($filePath,$key);
        $url = config('qiniu.domain') . $data;
        return json(['code'=>200,'msg'=>$url,'data'=>['src'=> $url]]);
    }

    public function uploadImg1(){
        $file = request()->file('input_image1');

        $dir = ROOT_PATH . 'public' . DS . 'uploads/home/images';
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
        $url =  http_type ();
        $info = $file->move($dir);
        if($info){
            $newName = $info->getSaveName();
            return json(['code'=>200,'msg'=>$url."/uploads/home/images/{$newName}",'data'=>['src'=> "$url/uploads/home/images/{$newName}"]]);
        }else{
            return json(['code'=>100,'msg'=>$file->getError(),'data'=>['src'=> ""]]);
        }
    }

    public function uploadImg2(){
        $file = request()->file('input_image2');

        $dir = ROOT_PATH . 'public' . DS . 'uploads/home/images';
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
        $url =  http_type ();
        $info = $file->move($dir);
        if($info){
            $newName = $info->getSaveName();
            return json(['code'=>200,'msg'=>$url."/uploads/home/images/{$newName}",'data'=>['src'=> "$url/uploads/home/images/{$newName}"]]);
        }else{
            return json(['code'=>100,'msg'=>$file->getError(),'data'=>['src'=> ""]]);
        }
    }
    public function uploadImg3(){
        $file = request()->file('input_image3');

        $dir = ROOT_PATH . 'public' . DS . 'uploads/home/images';
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
        $url =  http_type ();
        $info = $file->move($dir);
        if($info){
            $newName = $info->getSaveName();
            return json(['code'=>200,'msg'=>$url."/uploads/home/images/{$newName}",'data'=>['src'=> "$url/uploads/home/images/{$newName}"]]);
        }else{
            return json(['code'=>100,'msg'=>$file->getError(),'data'=>['src'=> ""]]);
        }
    }
    /**
     * 上传图片
     */
    public function uploadImage(){
        //引入getid3媒体类
        set_time_limit (0);
        if(!$_FILES['file']){
            return json(['code'=>100,'msg'=>'请选择图片']);
        }
        $accessKey = config('qiniu')['accessKey'];
        $secretKey = config('qiniu')['secretKey'];
        $bucket = config('qiniu')['bucket'];
        $auth = new Auth($accessKey, $secretKey);
        $type = explode(".",$_FILES['file']['name']);
        $type = end($type);
        $key = date('YmdHis') . rand(100000, 999999) . '.' . $type;//上传到七牛后的文件名称
        $returnBody = '{"key":"$(key)","vtime":"$(avinfo.video.duration)"}';//获取视频时长
        $policy = array(
            'returnBody' => $returnBody
        );
        $filePath = $_FILES['file']['tmp_name'];
        $expires = 3600;
        $token = $auth->uploadToken($bucket, null, $expires, $policy, true);// 生成上传 Token
        $uploadMgr = new UploadManager();// 初始化 UploadManager 对象并进行文件的上传。
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);// 调用 UploadManager 的 putFile 方法进行文件的上传。
        if($err !== null) {
            echo '上传失败，请重试';
        } else {
            echo "http://qiniu.shunhezhaofang.com/" . $ret['key'];
        }

    }
    /**
     * 上传视频
     */
    public function uploadVideo(){
        //引入getid3媒体类
        set_time_limit (0);
        vendor('getid3.getid3');
        $file = request()->file('input_video');
        if(!$file){
            return json(['code'=>100,'msg'=>'请选择视频']);
        }
        $info  = $file->getInfo();
        $size  = $file->getSize();
        if(($size/1024/1024) > 80){
            return json(['code'=>100,'msg'=>'视频大小不得超过80M']);
        }
        $mp3 = new \getID3();
        $filePath = $info['tmp_name'];
        $info =  $mp3->analyze($info['tmp_name']);
        $long = (int)floor($info['playtime_seconds']);//获取上传视频时长
        if($long < 30){
            return json(['code'=>100,'msg'=>'请上传30s以上的视频']);
        }
        $accessKey = config('qiniu')['accessKey'];
        $secretKey = config('qiniu')['secretKey'];
        $bucket = config('qiniu')['bucket'];
        $auth = new Auth($accessKey, $secretKey);
        $key = date('YmdHis') . rand(100000, 999999) . '.mp4';//上传到七牛后的文件名称
        $returnBody = '{"key":"$(key)","vtime":"$(avinfo.video.duration)"}';//获取视频时长
        $policy = array(
            'returnBody' => $returnBody
        );
        $expires = 3600;
        $token = $auth->uploadToken($bucket, null, $expires, $policy, true);// 生成上传 Token
        $uploadMgr = new UploadManager();// 初始化 UploadManager 对象并进行文件的上传。
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);// 调用 UploadManager 的 putFile 方法进行文件的上传。
        if($err !== null) {
            return json(['code'=>100,'msg'=>'上传失败，请重试','data'=>['src'=> ""]]);
        } else {
            return json(['code'=>200,'msg'=>"http://qiniu.shunhezhaofang.com/" . $ret['key'],'data'=>['src'=> $ret['key']]]);
        }

    }

    /**
     * uploadLocality图片上传至本地&压缩
     */
    public function uploadLocality(){
        $file = request()->file('file');
        $dir = ROOT_PATH . 'public' . DS . 'uploads/images';
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
//        $url =  http_type ();
        $info = $file->move($dir);
        if($info){
            $newName = $info->getSaveName();
            //压缩图片
//            image_png_size_add(ROOT_PATH . 'public' . DS . 'uploads/images/'.$newName,ROOT_PATH . 'public' . DS . 'uploads/images/'.$newName);
            echo "/uploads/images/{$newName}";
        }else{
            echo $file->getError();
        }
    }

    /**
     * deleteLocality 删除本地图片
     * @return \think\response\Json
     */
    public function deleteLocality(){
        return json(['code'=>200,'msg'=>'删除成功！']);
        $add  = input('add');
        $add = substr ($add,1);
        if(unlink($add)){
            return json(['code'=>200,'msg'=>'删除成功！']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败！']);
        }
    }
    /*
        * wangEditor图片上传
        */
    public function wangUpload(){
        set_time_limit (0);


        $files = request()->file();
        $dir = ROOT_PATH . 'public' . DS . 'uploads/images';
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
        $url =  http_type ();
        foreach($files as $file){
            $info = $file->move($dir);
            if($info){
                $newName = $info->getSaveName();
                $data[] = $url."/uploads/images/{$newName}";
            }
        }
        return json(['errno'=>0,'data'=>$data]);
    }
}